package handler

import (
	"giftsend/api/request"
	"giftsend/inits"
	"giftsend/interface/models"
	"github.com/gin-gonic/gin"
)

func CommentCreate(context *gin.Context) {
	var req request.CommentCreate
	err := context.ShouldBind(&req)
	if err != nil {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  "评论发布失败",
			"data": err.Error(),
		})
		return
	}
	var zbj models.Zbj
	err = inits.DB.Where("id = ?", zbj.Id).Find(&zbj).Error
	if err != nil {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  "直播间不存在",
			"data": "",
		})
		return
	}
	Zbj := models.Comments{

		Context: req.Context,
		ZbjId:   req.ZbjId,
	}

	err = inits.DB.Create(&Zbj).Error
	if err != nil {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  "评论发布失败",
			"data": "",
		})
		return
	}

	context.JSON(200, gin.H{
		"code": "200",
		"msg":  "评论发布成功",
		"data": Zbj,
	})

}
func CommentUpdate(context *gin.Context) {
	var req request.CommentUpdate
	err := context.ShouldBind(&req)
	if err != nil {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  "评论修改失败",
			"data": err.Error(),
		})
		return
	}
	Comments := models.Comments{
		Id:      req.Id,
		Context: req.Context,
	}

	err = inits.DB.Where("id = ?", req.Id).Updates(&Comments).Error
	if err != nil {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  "评论修改失败",
			"data": "",
		})
		return
	}

	context.JSON(200, gin.H{
		"code": "200",
		"msg":  "评论修改成功",
		"data": Comments,
	})
}

func CommentDelete(context *gin.Context) {
	var req request.CommentDelete
	err := context.ShouldBind(&req)
	if err != nil {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  "评论删除成功",
			"data": err.Error(),
		})
		return
	}
	Comments := models.Comments{
		Id: req.Id,
	}

	err = inits.DB.Where("id = ?", req.Id).Delete(&Comments).Error
	if err != nil {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  "评论删除失败",
			"data": "",
		})
		return
	}

	context.JSON(200, gin.H{
		"code": "200",
		"msg":  "评论删除",
		"data": Comments,
	})
}

func CommentShow(context *gin.Context) {
	var req request.CommentShow
	err := context.ShouldBind(&req)
	if err != nil {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  "评论删除成功",
			"data": err.Error(),
		})
		return
	}
	var comment models.Comments

	err = inits.DB.Where("id = ?", req.Id).Find(&comment).Error
	if err != nil {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  "评论删除失败",
			"data": "",
		})
		return
	}

	context.JSON(200, gin.H{
		"code": "200",
		"msg":  "评论详情",
		"data": comment,
	})
}
